package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.entity.User;
import com.woniuxy.carrental.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户的controller
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    //分页查询客户
    @GetMapping("/selectUsers")
    public ResponseResult<PageBean<User>> getUsers(User user, int pageIndex, int pageSize) {
        PageBean<User> pageBean = new PageBean<>();

        try {
            //获取客户的总条数
            int i = userService.selectAllUserCount();

            //获取全部客户数据
            List<User> users = userService.selectUserByPage(pageIndex, pageSize, user);
            pageBean = new PageBean<>();
            pageBean.setBeanList(users);
            pageBean.setPageIndex(pageIndex);
            pageBean.setPageSize(pageSize);
            pageBean.setTotalRecord(i);

            return new ResponseResult<PageBean<User>>(pageBean, 200, "查询完成");
        } catch (Exception e) {

            return new ResponseResult<PageBean<User>>(pageBean, 404, "查询失败");
        }

    }

    /*
    拉入黑名单
     */

    @PutMapping("/updateUsers/{uid}")
    public ResponseResult updateUser(@PathVariable("uid") int uid) {


        System.out.println(uid);
        userService.updateUserByBlack(uid);
        return new ResponseResult<PageBean<User>>(null, 200, "成功");
    }
    /*
      移除黑名单
       */
    @PutMapping("/updateUserByWhite/{uid}")
    public ResponseResult updateUserByWhite( @PathVariable("uid") int uid ) {


        userService.updateUserByWhite(uid);
        System.out.println(uid);


        return new ResponseResult<PageBean<User>>(null, 200, "移除黑名单成功");
    }

    //修改用户
    @PutMapping("/updateUserAll")
    public ResponseResult updateUserAll(@RequestBody User user) {

        // ModelAndView mv=new ModelAndView();
        //默认不在黑名单
        //user.setUBlacklist(true);
        //设置默认账号
        user.setLoginname("nihao123");
        //设置默认密码
        user.setPassword("123321");
        userService.updateUserAll(user);
        System.out.println(user);


        return new ResponseResult<PageBean<User>>(null, 200, "修改成功");
    }

    //根据id查询用户
    @GetMapping("/selectUserAll/{uid}")
    public User selectUserByUid(@PathVariable("uid") Integer uid) {
        User user = userService.userByUid(uid);
        System.out.println(user);
        return user;
    }




    //添加客户
    @PostMapping("/addusers")
    public ResponseResult addStudent(@RequestBody User user) {

        user.setUid(null);
        user.setLoginname("aaaaaa");
        user.setPassword("xxxxxx");
        user.setUblacklist(true);
        userService.insertUser(user);

        System.out.println("添加成功" + user);
        return new ResponseResult<PageBean<User>>(null, 200, "添加成功");
    }
}
